package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MindMapRecordAdapter;
import flc.ast.databinding.ActivityMindMapRecordBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;

public class MindMapRecordActivity extends BaseAc<ActivityMindMapRecordBinding> {

    private MindMapRecordAdapter recordAdapter;
    private Dialog myMoreDialog;
    private Dialog myRenameDialog;
    private EditText editText;
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_mind_map_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMindMapRecordBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new MindMapRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivMindMapRecordItemMore);
        recordAdapter.setOnItemChildClickListener(this);
//        recordAdapter.setList(MindUtil.getSavedFiles());
        //更多弹框
        moreDialog();
        //重命名弹框
        renameDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            //更多弹框
            case R.id.tvDialogMoreRename://重命名
                myMoreDialog.dismiss();
                myRenameDialog.show();
                break;
            case R.id.tvDialogMoreSave://保存到手机
                myMoreDialog.dismiss();
                save();
                break;
            case R.id.tvDialogMoreDelete://删除
                myMoreDialog.dismiss();
                delete();
                break;
            case R.id.tvDialogMoreCancel://取消
                myMoreDialog.dismiss();
                break;
            //重命名
            case R.id.ivDialogRenameConfirm:
                String name = editText.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showShort(getText(R.string.please_input_file_name));
                    return;
                }
                FileUtils.rename(selPath, name + "." + FileUtils.getFileExtension(selPath));
                myRenameDialog.dismiss();
                editText.setText("");
                getData();
                break;
            case R.id.ivDialogRenameCancel:
                editText.setText("");
                myRenameDialog.dismiss();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivMindMapRecordItemMore:
                selPath = recordAdapter.getItem(position).getPath();
                myMoreDialog.show();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取记录数据
    private void getData() {
        String path = PathUtils.getFilesPathExternalFirst() + Content.myMindMapRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 3) {
            files = files.subList(0, 3);
        }
        if (files.size() > 0) {
            recordAdapter.setList(files);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //保存
    private void save() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileP2pUtil.copyPrivateImgToPublic(mContext, selPath);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.download_suc);
            }
        });
    }

    //删除
    private void delete() {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(selPath);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

    //更多弹框
    private void moreDialog() {
        myMoreDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_more, null);
        myMoreDialog.setContentView(inflate);
        myMoreDialog.setCancelable(false);
        Window dialogWindow = myMoreDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 80;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogRename = inflate.findViewById(R.id.tvDialogMoreRename);
        TextView dialogSave = inflate.findViewById(R.id.tvDialogMoreSave);
        TextView dialogDelete = inflate.findViewById(R.id.tvDialogMoreDelete);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogMoreCancel);
        dialogRename.setOnClickListener(this);
        dialogSave.setOnClickListener(this);
        dialogDelete.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

    //重命名弹框
    private void renameDialog() {
        myRenameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename, null);
        myRenameDialog.setContentView(inflate);
        myRenameDialog.setCancelable(true);
        Window dialogWindow = myRenameDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogRenameCancel);
        ImageView comp = inflate.findViewById(R.id.ivDialogRenameConfirm);
        editText = inflate.findViewById(R.id.etDialogFormatRename);
        cancel.setOnClickListener(this);
        comp.setOnClickListener(this);
    }

}